package com.hepinghospital.user.ui.activity.nursing;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.NursingProjectResponse;
import com.rest.response.QueryProtocolResponse;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import jz.joyoung.robot.util.UtilString;


/**
 * @Description: 预约护理 预约流程告知
 * @Author: lzp
 * @CreateDate: 2022/3/7 9:23
 * @UpdateRemark: 更新说明
 */
public class AppointStep1Activity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_appointment)
    TextView tvAppointment;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_subtitle)
    TextView tvSubtitle;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.webview)
    WebView webView;

    NursingProjectResponse.ChargeItemsBean chargeItemsBean;
    static WeakReference<Activity> mActivity = null;

    @Override
    protected void initViews() {
        tvTitle.setText("护理预约");
        tvAppointment.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("item", chargeItemsBean);
            startActivity(AppointStep2Activity.class, bundle);
        });

        //可以设置webview没得 滚动条
        webView.setBackgroundColor(0);
//        webView.getBackground().setAlpha( 2 );
        webView.getSettings().setJavaScriptEnabled(false);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setTextZoom(300);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        webView.getSettings().setLoadWithOverviewMode(true);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        mActivity = new WeakReference<>(this);
        chargeItemsBean = (NursingProjectResponse.ChargeItemsBean) getIntent().getSerializableExtra("item");
        tvName.setText(chargeItemsBean.getChargeItemName());
//        tvName.setText(chargeItemsBean.getChargeItemName().substring(0, chargeItemsBean.getChargeItemName().indexOf("（")));
//        tvSubtitle.setText(chargeItemsBean.getChargeItemName().substring(chargeItemsBean.getChargeItemName().indexOf("（") + 1, chargeItemsBean.getChargeItemName().length() - 1));
        tvPrice.setText(UtilString.stringToDouble(chargeItemsBean.getPrice()) + "元/" + chargeItemsBean.getUnit() + "起");

        RestProxy.getInstance().queryProtocol(chargeItemsBean.getChargeItemCode(), new BaseObserver<QueryProtocolResponse>() {
            @Override
            public void onSuccess(QueryProtocolResponse baseResponse) {
                webView.loadDataWithBaseURL(null, baseResponse.text, "text/html", "utf-8", null);
            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_nursing_appoint_step1;
    }

    public static void clsoeSelf() {
        if (mActivity != null && mActivity.get() != null) {
            Activity context = mActivity.get();
            context.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mActivity != null) {
            mActivity.clear();
            mActivity = null;
        }
    }
}
